home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Sessions / Traut / ZStrings / Source / MacOS / GraphicalTool / FileIconPane.cp < prev    next >
Encoding:
Text File  |  2001-06-23  |  3.2 KB  |  133 lines

  1. /*==================================================================
  2.     File:        FileIconPane.cp
  3.  
  4.     Contains:    Implementation of a simple pane that draws the icon
  5.                 associated with a file (specified as either an IconRef
  6.                 or a color icon ID).
  7.  
  8.     Written by:    Eric Traut
  9.  
  10.     Copyright:    2000-2001 Connectix Corporation
  11.     
  12.     This source has been placed into the public domain by
  13.     Connectix Corporation. You have the right to modify, 
  14.     distribute or use this code without any legal limitations
  15.     or finanicial/licensing requirements. Connectix is not 
  16.     liable for any problems that result from the use of this 
  17.     code.
  18.     
  19.     If you have comments, feedback, questions, or would like
  20.     to submit bug fixes or updates to this code, please email
  21.     opensource@connectix.com.
  22. ==================================================================*/
  23.  
  24. // ===========================================================================
  25. //    FileIconPane.cp
  26. // ===========================================================================
  27.  
  28. #pragma once
  29.  
  30. #include "FileIconPane.h"
  31.  
  32. #include <Icons.h>
  33.  
  34.  
  35. // ---------------------------------------------------------------------------
  36. //    • FileIconPane                            Stream Constructor          [public]
  37. // ---------------------------------------------------------------------------
  38.  
  39. FileIconPane::FileIconPane(
  40.     LStream *        inStream)
  41.     : LPane(inStream)
  42. {
  43.     mIconRef = NULL;
  44.     mUseIconID = true;
  45.     mIconID = icon_UnknownFileTypeIcon;
  46. }
  47.  
  48.  
  49. // ---------------------------------------------------------------------------
  50. //    • ~FileIconPane                            Destructor                  [public]
  51. // ---------------------------------------------------------------------------
  52.  
  53. FileIconPane::~FileIconPane()
  54. {
  55.  
  56. }
  57.         
  58. // ---------------------------------------------------------------------------
  59. //    • SetIconRef                                                      [public]
  60. // ---------------------------------------------------------------------------
  61.  
  62. void
  63. FileIconPane::SetIconRef(
  64.     IconRef        inIconRef)
  65. {
  66.     mIconRef = inIconRef;
  67.     mUseIconID = false;
  68.     
  69.     Refresh();
  70. }
  71.  
  72.  
  73. // ---------------------------------------------------------------------------
  74. //    • SetIconID                                                      [public]
  75. // ---------------------------------------------------------------------------
  76.  
  77. void
  78. FileIconPane::SetIconID(
  79.     ResID        inColorIconID)
  80. {
  81.     mUseIconID = true;
  82.     mIconID = inColorIconID;
  83.     
  84.     Refresh();
  85. }
  86.  
  87.  
  88. // ---------------------------------------------------------------------------
  89. //    • DrawSelf                                                          [public]
  90. // ---------------------------------------------------------------------------
  91.  
  92. void
  93. FileIconPane::DrawSelf()
  94. {
  95.     Rect        boundsRect;
  96.     CalcLocalFrameRect(boundsRect);
  97.  
  98.     if (!mUseIconID)
  99.     {
  100.         if (mIconRef != NULL && ::IsValidIconRef(mIconRef))
  101.         {
  102.             // Plot the icon in the center
  103.             OSErr err = ::PlotIconRef(
  104.                 &boundsRect,
  105.                 atAbsoluteCenter,
  106.                 IsEnabled() && IsActive() ? ttNone : ttDisabled,
  107.                 kIconServicesNormalUsageFlag,
  108.                 mIconRef);
  109.             SignalIf_(err != noErr);
  110.         }
  111.     }
  112.     else
  113.     {
  114.         CIconHandle iconHandle = ::GetCIcon(mIconID);
  115.         SignalIf_(iconHandle == NULL);
  116.         
  117.         if (iconHandle != NULL)
  118.         {
  119.             // Plot a generic icon in the center
  120.             OSErr err = ::PlotCIconHandle(
  121.                 &boundsRect,
  122.                 atAbsoluteCenter,
  123.                 IsEnabled() && IsActive() ? ttNone : ttDisabled,
  124.                 iconHandle);
  125.             SignalIf_(err != noErr);
  126.             
  127.             ::DisposeCIcon(iconHandle);
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133.